home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / basic / heap1.c next >
Encoding:
C/C++ Source or Header  |  1999-01-04  |  917 b   |  42 lines

  1. /* demonstrates dynamic overflow on heap (initialized data) */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. #define ERROR -1
  10. #define BUFSIZE 16
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.    u_long diff;
  15.    u_int oversize;
  16.    register int i;
  17.  
  18.    char *buf1 = (char *)malloc(BUFSIZE), *buf2 = (char *)malloc(BUFSIZE);
  19.  
  20.    if (argc <= 1)
  21.    {
  22.       fprintf(stderr, "Usage: %s <numbytes>\n", argv[0]);
  23.       fprintf(stderr, "[Will overflow dynamic buffer by <numbytes>]\n");
  24.        
  25.       exit(ERROR);
  26.    }
  27.  
  28.    diff = (u_long)buf2 - (u_long)buf1;
  29.    printf("buf1 = %p, buf2 = %p, diff = 0x%x bytes\n", buf1, buf2, diff);
  30.  
  31.    memset(buf2, 'A', BUFSIZE-1);
  32.    buf2[BUFSIZE-1] = '\0';
  33.  
  34.    printf("before overflow: buf2 = %s\n", buf2);
  35.  
  36.    oversize = (u_int)(diff + atoi(argv[1]));
  37.    memset(buf1, 'B', oversize);
  38.  
  39.    printf("after overflow: buf2 = %s\n", buf2);
  40.    return 0;
  41. }
  42.